import java.awt.Color;
import java.io.FileInputStream;
import java.util.Properties;

import javax.swing.JOptionPane;

/*
 * GUI_Log_in.java
 *
 * Created on December 5, 2007, 2:48 PM
 */



/**
 *
 * @author  w00t
 */
public class GUI_Log_In extends javax.swing.JDialog {
    
    /** Creates new form GUI_Log_in */
    public GUI_Log_In(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        labelName = new javax.swing.JLabel();
        labelPassword = new javax.swing.JLabel();
        fieldName = new javax.swing.JTextField();
        fieldPassword = new javax.swing.JPasswordField();
        buttonClear = new javax.swing.JButton();
        buttonCancel = new javax.swing.JButton();
        buttonLogIn = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        labelName.setText("user name:"); // NOI18N
        labelName.setName("labelName"); // NOI18N

        labelPassword.setText("password:"); // NOI18N
        labelPassword.setName("labelPassword"); // NOI18N

        fieldName.setText(""); // NOI18N
        fieldName.setName("fieldName"); // NOI18N

        fieldPassword.setText(""); // NOI18N
        fieldPassword.setName("fieldPassword"); // NOI18N

        buttonClear.setText("clear"); // NOI18N
        buttonClear.setName("buttonClear"); // NOI18N
        buttonClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonClearActionPerformed(evt);
            }
        });

        buttonCancel.setText("cancel"); // NOI18N
        buttonCancel.setName("buttonCancel"); // NOI18N
        buttonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCancelActionPerformed(evt);
            }
        });

        buttonLogIn.setText("log in"); // NOI18N
        buttonLogIn.setName("buttonLogIn"); // NOI18N
        buttonLogIn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonLogInActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(labelName)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(fieldName, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(labelPassword)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(fieldPassword)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(buttonClear)
                        .addGap(18, 18, 18)
                        .addComponent(buttonCancel)
                        .addGap(18, 18, 18)
                        .addComponent(buttonLogIn)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelName)
                    .addComponent(fieldName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelPassword)
                    .addComponent(fieldPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonClear)
                    .addComponent(buttonCancel)
                    .addComponent(buttonLogIn))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pack();
        setResizable(false);
    }// </editor-fold>

    private void buttonClearActionPerformed(java.awt.event.ActionEvent evt) {
        fieldName.setText("");
        fieldPassword.setText("");
    }

    private void buttonCancelActionPerformed(java.awt.event.ActionEvent evt) {
       dispose();
    }

    private void buttonLogInActionPerformed(java.awt.event.ActionEvent evt) {
    	
    	String userName=fieldName.getText();		
    	
		char[] field1=fieldPassword.getPassword();
		String password = new String(field1);

		if(userName.length()==0){
			JOptionPane.showMessageDialog(this,"user name must be at least 1 character.","username error", JOptionPane.DEFAULT_OPTION);
			return;
		}

		if(password.length()<6){
			JOptionPane.showMessageDialog(this,"password must be at least 6 characters long.","passwrd error", JOptionPane.DEFAULT_OPTION);
			return;
		}
		//try to retrieve account
		Properties temp_config = new Properties();
		try {
			temp_config.load(new FileInputStream(userName+".config"));
		} catch (Exception e1) {
			System.err.println("failed to load configuration file: " + e1.getMessage());
			System.exit(1);
		}
		
		
		String password2 = temp_config.getProperty("password");
		if(!password.equals(password2)){
			JOptionPane.showMessageDialog(this,"passwords do not match.","password error", JOptionPane.DEFAULT_OPTION);
			return;
		}

		//read values off config file, and set up accordingly
		String name = temp_config.getProperty("name");
		int colorRGB = Integer.parseInt(temp_config.getProperty("color"));
		ClientController.localPlayer = new Player(name, new Color(colorRGB));
		

		
		ClientController.config = temp_config;
		dispose();
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                GUI_Log_In dialog = new GUI_Log_In(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify
    private javax.swing.JButton buttonCancel;
    private javax.swing.JButton buttonClear;
    private javax.swing.JButton buttonLogIn;
    private javax.swing.JTextField fieldName;
    private javax.swing.JPasswordField fieldPassword;
    private javax.swing.JLabel labelName;
    private javax.swing.JLabel labelPassword;
    // End of variables declaration
    
}
